package sy.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import sy.pageModel.DataGrid;
import sy.pageModel.Json;
import sy.pageModel.SessionInfo;
import sy.pageModel.SupplierInfo;
import sy.service.SupplierInfoServiceI;
import sy.util.IpUtil;
import sy.util.ResourceUtil;

@Controller
@RequestMapping("/supplierInfoController")
public class SupplierInfoController {

	private SupplierInfoServiceI supplierInfoService;

	public SupplierInfoServiceI getSupplierInfoService() {
		return supplierInfoService;
	}

	@Autowired
	public void setSupplierInfoService(SupplierInfoServiceI supplierInfoService) {
		this.supplierInfoService = supplierInfoService;
	}

	@InitBinder
	public void initBinder(ServletRequestDataBinder binder) {
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
	}

	@RequestMapping("/reg")
	@ResponseBody
	public Json reg(SupplierInfo supplierInfo) {
		Json j = new Json();
		try {
			SupplierInfo supplier = supplierInfoService.save(supplierInfo);
			j.setSuccess(true);
			j.setMsg("注册成功！");
			j.setObj(supplier);
		} catch (Exception e) {
			j.setMsg("注册失败！用户名已存在！");
		}
		return j;
	}

/*	@RequestMapping("/login")
	@ResponseBody
	public Json login(SupplierInfo SupplierInfo, HttpServletRequest request, HttpSession session) {
		Json j = new Json();
		SupplierInfo u = SupplierInfoService.find(SupplierInfo);
		if (u != null) {
			j.setSuccess(true);
			j.setMsg("登录成功！");

			SessionInfo sessionInfo = new SessionInfo();
			sessionInfo.setSupplierInfoId(u.getId());
			sessionInfo.setLoginName(u.getName());
			sessionInfo.setIp(IpUtil.getIpAddr(request));
			sessionInfo.setRoleIds(u.getRoleIds());
			sessionInfo.setRoleNames(u.getRoleNames());
			sessionInfo.setResourceIds(u.getResourceIds());
			sessionInfo.setResourceNames(u.getResourceNames());
			sessionInfo.setResourceUrls(u.getResourceUrls());
			session.setAttribute(ResourceUtil.getSessionInfoName(), sessionInfo);

			j.setObj(sessionInfo);
		} else {
			j.setMsg("登录失败！");
		}
		return j;
	}*/

/*	@RequestMapping("/logout")
	@ResponseBody
	public Json logout(HttpSession session) {
		if (session != null) {
			session.invalidate();
		}
		Json j = new Json();
		j.setSuccess(true);
		return j;
	}
*/
	@RequestMapping("/combogrid")
	@ResponseBody
	public DataGrid combogrid(SupplierInfo supplierInfo) {
		System.out.println("测试***combogrid");
		return supplierInfoService.datagrid(supplierInfo);
	}

	@RequestMapping("/combobox")
	@ResponseBody
	public List<SupplierInfo> combobox(SupplierInfo supplierInfo) {
		return supplierInfoService.combobox(supplierInfo);
	}

	@RequestMapping("/datagrid")
	@ResponseBody
	public DataGrid datagrid(SupplierInfo supplierInfo) {
		System.out.println("测试datagrid***"+supplierInfo.getSupplierzhname());
		return supplierInfoService.datagrid(supplierInfo);
	}

	@RequestMapping("/remove")
	@ResponseBody
	public Json remove(SupplierInfo supplierInfo) {
		System.out.println("/remove******delete");
		supplierInfoService.remove(supplierInfo.getIds());
		Json j = new Json();
		j.setSuccess(true);
		j.setMsg("删除成功！");
		return j;
	}

	@RequestMapping("/add")
	@ResponseBody
	public Json add(SupplierInfo supplierInfo) {
		Json j = new Json();
		try {
			System.out.println("/add****");
			SupplierInfo supplier = supplierInfoService.save(supplierInfo);
			j.setSuccess(true);
			j.setMsg("添加成功！");
			j.setObj(supplier);
		} catch (Exception e) {
			j.setMsg("添加失败！供应商已存在！");
		}
		return j;
	}

	@RequestMapping("/edit")
	@ResponseBody
	public Json edit(SupplierInfo supplierInfo) {
		System.out.println("edit*000*********");
		Json j = new Json();
		try {
			SupplierInfo supplier = supplierInfoService.edit(supplierInfo);
			j.setSuccess(true);
			j.setMsg("编辑成功！");
			j.setObj(supplier);
		} catch (Exception e) {
			j.setMsg("编辑失败！用户名已存在！");
		}
		return j;
	}

/*	@RequestMapping("/modifyRole")
	@ResponseBody
	public Json modifyRole(SupplierInfo SupplierInfo) {
		Json j = new Json();
		SupplierInfoService.modifyRole(SupplierInfo);
		j.setSuccess(true);
		j.setMsg("编辑成功！");
		return j;
	}
*/
	/*	@RequestMapping("/modifyPwd")
	@ResponseBody
	public Json modifyPwd(SupplierInfo SupplierInfo) {
		Json j = new Json();
		SupplierInfoService.modifyPwd(SupplierInfo);
		j.setSuccess(true);
		j.setMsg("编辑成功！");
		return j;
	}*/

/*	@RequestMapping("/modifySupplierInfo")
	@ResponseBody
	public Json modifyCurrentSupplierInfoPwd(SupplierInfo SupplierInfo) {
		Json j = new Json();
		supplierInfoService.modifyPwd(supplierInfo);
		j.setSuccess(true);
		j.setMsg("修改成功！");
		return j;
	}*/
/*
	@RequestMapping("/supplierInfoInfo")
	public String supplierInfoInfo() {
		return "supplierInfo/supplierInfoInfo";
	}*/

}
